import Vue from 'vue'
import VueRouter from 'vue-router'
import NotFound from '@/views/NotFound'
import Login from '@/views/Login'
import Register from '@/views/Register'
import Index from '@/views/Index'

import { Message } from 'element-ui'

// IndexMain Children
import MainHome from '@/components/MainHome'
import MainTable from '@/components/MainTable'
import MainUserInfo from '@/components/MainUserInfo'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/index',
    // name: 'Index',
    component: Index,
    children: [{
        path: '',
        redirect: 'home',
      },
      {
        path: 'home',
        name: 'home',
        component: MainHome,
      },
      {
        path: 'table',
        name: 'table',
        component: MainTable,
      },
      {
        path: 'userinfo',
        name: 'userinfo',
        component: MainUserInfo,
      },
    ]
  },
  {
    path: '*',
    name: 'NotFound',
    component: NotFound
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由拦截
router.beforeEach((to, from, next) => {
  let isLogin = localStorage.moneyToken ? true : false;
  if (to.path == '/login' || to.path == '/register') {
    next();
  } else {
    if (isLogin) {
      next();
    } else {
      Message.error('请登录');
      next({ name: 'Login' });
    }
  }
})

export default router